package com.holyn.guofang.presenter.my;

import com.holyn.guofang.MyApplication;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.RetrofitManager;
import com.holyn.guofang.model.webhttp.observable.CardObservable;
import com.holyn.guofang.model.webhttp.observable.LogObservable;
import com.holyn.guofang.model.webhttp.observable.OrderObservable;
import com.holyn.guofang.model.webhttp.observable.PayObservable;
import com.holyn.guofang.model.webhttp.observable.UserLoginedObservable;
import com.holyn.guofang.model.webhttp.observable.UserUnLoginObservable;
import com.holyn.guofang.model.webhttp.wo.AddCardOrderWo;
import com.holyn.guofang.model.webhttp.wo.AttachedCardDictionaryWo;
import com.holyn.guofang.model.webhttp.wo.CardDictionaryWo;
import com.holyn.guofang.model.webhttp.wo.CardOrderDetailWo;
import com.holyn.guofang.model.webhttp.wo.CouponInfoWo;
import com.holyn.guofang.model.webhttp.wo.DeviceWo;
import com.holyn.guofang.model.webhttp.wo.LogItemWo;
import com.holyn.guofang.model.webhttp.wo.OrderItemWo;
import com.holyn.guofang.model.webhttp.wo.PaySignWo;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.model.webhttp.wo.UpdateInfoWo;
import com.holyn.guofang.model.webhttp.wo.UserCardListWo;
import com.holyn.guofang.model.webhttp.wo.UserInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.mylibrary.utils.AppUtil;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by Holyn on 2017/8/9.
 */

public class MyPresenter extends MyContract.Presenter{
    private UserLoginedObservable loginedObservable;
    private UserUnLoginObservable userUnLoginObservable;
    private CardObservable cardObservable;
    private OrderObservable orderObservable;
    private LogObservable logObservable;
    private PayObservable payObservable;

    private MyContract.RegisterInfoView registerInfoView = null;
    private MyContract.SecondaryDeviceListView secondaryDeviceListView = null;
    private MyContract.UpdateCheckMethodView updateCheckMethodView = null;
    private MyContract.ChangeDeviceView changeDeviceView = null;
    private MyContract.UpdatePasswordView updatePasswordView = null;
    private MyContract.CardManagerView cardManagerView = null;
    private MyContract.UpdateValidMethodView updateValidMethodView = null;
    private MyContract.RechargeView rechargeView = null;
    private MyContract.HomeListenenr homeListenenr = null;
    private MyContract.OrderDetailView orderDetailView = null;
    private MyContract.OrderListView orderListView = null;
    private MyContract.UserLogListView userLogListView = null;

    public MyPresenter(BaseView baseView) {
        super(baseView);
        loginedObservable = RetrofitManager.getCommonObservable(UserLoginedObservable.class);
        userUnLoginObservable = RetrofitManager.getCommonObservable(UserUnLoginObservable.class);
        cardObservable = RetrofitManager.getCommonObservable(CardObservable.class);
        orderObservable = RetrofitManager.getCommonObservable(OrderObservable.class);
        logObservable = RetrofitManager.getCommonObservable(LogObservable.class);
        payObservable = RetrofitManager.getCommonObservable(PayObservable.class);
    }

    @Override
    public void addRegisterInfoViewListener(MyContract.RegisterInfoView registerInfoView) {
        this.registerInfoView = registerInfoView;
    }

    @Override
    public void addSecondaryDeviceListView(MyContract.SecondaryDeviceListView secondaryDeviceListView) {
        this.secondaryDeviceListView = secondaryDeviceListView;
    }

    @Override
    public void addUpdateCheckMethodView(MyContract.UpdateCheckMethodView updateCheckMethodView) {
        this.updateCheckMethodView = updateCheckMethodView;
    }

    @Override
    public void addChangeDeviceView(MyContract.ChangeDeviceView changeDeviceView) {
        this.changeDeviceView = changeDeviceView;
    }

    @Override
    public void addUpdatePasswordView(MyContract.UpdatePasswordView updatePasswordView) {
        this.updatePasswordView = updatePasswordView;
    }

    @Override
    public void addCardManagerView(MyContract.CardManagerView cardManagerView) {
        this.cardManagerView = cardManagerView;
    }

    @Override
    public void addUpdateValidMethodView(MyContract.UpdateValidMethodView updateValidMethodView) {
        this.updateValidMethodView = updateValidMethodView;
    }

    @Override
    public void addRechargeView(MyContract.RechargeView rechargeView) {
        this.rechargeView = rechargeView;
    }

    @Override
    public void addOrderDetailView(MyContract.OrderDetailView orderDetailView) {
        this.orderDetailView = orderDetailView;
    }

    @Override
    public void addOrderListView(MyContract.OrderListView orderListView) {
        this.orderListView = orderListView;
    }

    @Override
    public void addUserLogListView(MyContract.UserLogListView userLogListView) {
        this.userLogListView = userLogListView;
    }

    @Override
    public void addHomeListenenr(MyContract.HomeListenenr homeListenenr) {
        this.homeListenenr = homeListenenr;
    }

    @Override
    public void postUserInfo(int requestAction) {
        getCommonObservable(requestAction, loginedObservable.userInfo(""))
                .subscribe(new Consumer<UserInfoWo>() {
                    @Override
                    public void accept(@NonNull UserInfoWo userInfoWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, userInfoWo);
                        }
                        if (changeDeviceView != null){
                            changeDeviceView.responseSuccess(requestAction, userInfoWo);
                        }
                    }
                });
    }

    @Override
    public void postUpdateUserInfo(int requestAction, String name, String mobile, String email, String address, String country, String area, String contact, String contactTel, String contactMobile, String contactEmail, String checkMethod, String newCheckMethod, String unBindCode, String bindCode) {
        getCommonObservable(requestAction, loginedObservable.updateUserInfo(name, mobile, email, address, country, area, contact, contactTel, contactMobile, contactEmail, checkMethod, newCheckMethod, unBindCode, bindCode))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, statusWo);
                        }
                        if (updateCheckMethodView != null){
                            updateCheckMethodView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });
    }

    @Override
    public void postUnBindCode(int requestAction) {
        getCommonObservable(requestAction, loginedObservable.getUnBindCode(""))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (updateCheckMethodView != null){
                            updateCheckMethodView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });
    }

    @Override
    public void postBindCode(int requestAction, String checkMethod, String equipmentCode) {
        getCommonObservable(requestAction, loginedObservable.getBindCode(checkMethod, equipmentCode))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (updateCheckMethodView != null){
                            updateCheckMethodView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });

    }

    @Override
    public void postDeviceList(int requestAction, int mainDevice) {
        getCommonObservable(requestAction, loginedObservable.deviceList(mainDevice))
                .subscribe(new Consumer<DeviceWo>() {
                    @Override
                    public void accept(@NonNull DeviceWo deviceWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, deviceWo);
                        }
                        if (secondaryDeviceListView != null){
                            secondaryDeviceListView.responseSuccess(requestAction, deviceWo);
                        }
                        if (changeDeviceView != null){
                            changeDeviceView.responseSuccess(requestAction, deviceWo);
                        }
                    }
                });

    }

    @Override
    public void postDeviceListB(int requestAction) {
        getCommonObservable(requestAction, loginedObservable.deviceListb(0))
                .subscribe(new Consumer<DeviceWo>() {
                    @Override
                    public void accept(@NonNull DeviceWo deviceWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, deviceWo);
                        }
                        if (secondaryDeviceListView != null){
                            secondaryDeviceListView.responseSuccess(requestAction, deviceWo);
                        }
                        if (changeDeviceView != null){
                            changeDeviceView.responseSuccess(requestAction, deviceWo);
                        }
                    }
                });
    }

    @Override
    public void postUpdateDeviceRemark(int requestAction, Integer deviceID, String remark) {
        getCommonObservable(requestAction, loginedObservable.updateDeviceRemark(deviceID, remark))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, statusWo);
                        }
                        if (secondaryDeviceListView != null){
                            secondaryDeviceListView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });

    }

    @Override
    public void postUpdateDeviceRemarkB(int requestAction, Integer deviceID, String remark) {
        getCommonObservable(requestAction, loginedObservable.updateDeviceRemarkb(deviceID, remark))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, statusWo);
                        }
                        if (secondaryDeviceListView != null){
                            secondaryDeviceListView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });

    }

    @Override
    public void postUpdateAuthorize(int requestAction, int deviceID) {
        getCommonObservable(requestAction, loginedObservable.updateAuthorize(deviceID))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, statusWo);
                        }
                        if (secondaryDeviceListView != null){
                            secondaryDeviceListView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });

    }

    @Override
    public void postUpdateAuthorizeB(int requestAction, int deviceID) {
        getCommonObservable(requestAction, loginedObservable.updateAuthorizeb(deviceID))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, statusWo);
                        }
                        if (secondaryDeviceListView != null){
                            secondaryDeviceListView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });

    }

    @Override
    public void postDeleteDevice(int requestAction, int deviceID) {
        getCommonObservable(requestAction, loginedObservable.deleteDevice(deviceID))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, statusWo);
                        }
                        if (secondaryDeviceListView != null){
                            secondaryDeviceListView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });

    }

    @Override
    public void postUpdateAuthorizeStop(int requestAction, int deviceID) {
        getCommonObservable(requestAction, loginedObservable.updateAuthorizeStop(deviceID))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (registerInfoView != null){
                            registerInfoView.responseSuccess(requestAction, statusWo);
                        }
                        if (secondaryDeviceListView != null){
                            secondaryDeviceListView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });

    }

    @Override
    public void postMainDeviceCode(int requestAction) {
        getCommonObservable(requestAction, loginedObservable.getMainDeviceCode(""))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (changeDeviceView != null){
                            changeDeviceView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });
    }

    @Override
    public void postUpdateMainDevice(int requestAction, int deviceID, String validCode) {
        getCommonObservable(requestAction, loginedObservable.updateMainDevice(deviceID, validCode))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (changeDeviceView != null){
                            changeDeviceView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });
    }

    @Override
    public void postUserCardList(int requestAction, Integer activeState, Integer pageSize, Integer page) {
        getCommonObservable(requestAction, cardObservable.userCardList(activeState, pageSize, page))
                .subscribe(new Consumer<UserCardListWo>() {
                    @Override
                    public void accept(@NonNull UserCardListWo userCardListWo) throws Exception {
                        if (changeDeviceView != null){
                            changeDeviceView.responseSuccess(requestAction, userCardListWo);
                        }
                        if (cardManagerView != null){
                            cardManagerView.responseSuccess(requestAction, userCardListWo);
                        }
                    }
                });

    }

    @Override
    public void postNewPasswordCode(int requestAction) {
        getCommonObservable(requestAction, loginedObservable.getNewPasswordCode(""))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        updatePasswordView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postUpdatePassword(int requestAction, String oldPassword, String newPassword, String validCode) {
        getCommonObservable(requestAction, loginedObservable.updatePassword(oldPassword, newPassword, validCode))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        updatePasswordView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postActiveCard(int requestAction, Integer cardID, String beginDate, Integer validMethod, String password) {
        getCommonObservable(requestAction, cardObservable.activeCard(cardID, beginDate, validMethod, password))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        cardManagerView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postValidMethodCode(int requestAction) {
        getCommonObservable(requestAction, cardObservable.getValidMethodCode(""))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        updateValidMethodView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postUpdateValidMethod(int requestAction, Integer cardID, Integer validMethod, String validCode, String password) {
        getCommonObservable(requestAction, cardObservable.updateValidMethod(cardID, validMethod, validCode, password))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        updateValidMethodView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postListCardDictionary(int requestAction, Integer currency, Integer pageSize, Integer page) {
        getCommonObservable(requestAction, cardObservable.listCardDictionary(currency, pageSize, page))
                .subscribe(new Consumer<CardDictionaryWo>() {
                    @Override
                    public void accept(@NonNull CardDictionaryWo cardDictionaryWo) throws Exception {
                        rechargeView.responseSuccess(requestAction, cardDictionaryWo);
                    }
                });
    }

    @Override
    public void postListAttachedCardDictionary(int requestAction, Integer parentID) {
        getCommonObservable(requestAction, cardObservable.listAttachedCardDictionary(parentID))
                .subscribe(new Consumer<AttachedCardDictionaryWo>() {
                    @Override
                    public void accept(@NonNull AttachedCardDictionaryWo attachedCardDictionaryWo) throws Exception {
                        rechargeView.responseSuccess(requestAction, attachedCardDictionaryWo);
                    }
                });
    }

    @Override
    public void postCouponInfo(int requestAction, Integer type, Integer cardCount) {
        getCommonObservable(requestAction, cardObservable.getCouponInfo(type, cardCount))
                .subscribe(new Consumer<CouponInfoWo>() {
                    @Override
                    public void accept(@NonNull CouponInfoWo couponInfoWo) throws Exception {
                        rechargeView.responseSuccess(requestAction, couponInfoWo);
                    }
                });
    }

    @Override
    public void postAddCardOrder(int requestAction, Integer cardID, Integer attenedCardID, Integer attenedCardCount,
                                 Integer needCoupon, Integer payWay, String couponCardNum, Integer couponCardID, String remark) {
        getCommonObservable(requestAction, cardObservable.addCardOrder(cardID, attenedCardID, attenedCardCount,
                needCoupon, payWay, couponCardNum, couponCardID, remark))
                .subscribe(new Consumer<AddCardOrderWo>() {
                    @Override
                    public void accept(@NonNull AddCardOrderWo addCardOrderWo) throws Exception {
                        rechargeView.responseSuccess(requestAction, addCardOrderWo);
                    }
                });
    }

    @Override
    public void postCardOrderDetail(int requestAction, Integer id) {
        getCommonObservable(requestAction, cardObservable.cardOrderDetail(id))
                .subscribe(new Consumer<CardOrderDetailWo>() {
                    @Override
                    public void accept(@NonNull CardOrderDetailWo cardOrderDetailWo) throws Exception {
                        orderDetailView.responseSuccess(requestAction, cardOrderDetailWo);
                    }
                });
    }

    @Override
    public void postDeleteOrder(int requestAction, Integer id) {
        getCommonObservable(requestAction, orderObservable.deleteOrder(id))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        orderDetailView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postUpdateOfflinePayStatus(int requestAction, Integer id) {
        getCommonObservable(requestAction, orderObservable.updateOfflinePayStatus(id))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        orderDetailView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postUpdateOrderPayStatus(int requestAction, Integer id) {
        getCommonObservable(requestAction, orderObservable.updateOrderPayStatus(id))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        orderDetailView.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postAlipaySign(int requestAction, String content) {
        getCommonObservable(requestAction, payObservable.GetAlipaySign(content))
                .subscribe(new Consumer<PaySignWo>() {
                    @Override
                    public void accept(@NonNull PaySignWo paySignWo) throws Exception {
                        orderDetailView.responseSuccess(requestAction, paySignWo);
                    }
                });
    }

    @Override
    public void postOrderList(int requestAction, String from, String to, Integer payStatus, Integer orderType, Integer pageSize, Integer page) {
        getCommonObservable(requestAction, orderObservable.orderList(from, to, payStatus, orderType, pageSize, page))
                .subscribe(new Consumer<OrderItemWo>() {
                    @Override
                    public void accept(@NonNull OrderItemWo orderItemWo) throws Exception {
                        orderListView.responseSuccess(requestAction, orderItemWo);
                    }
                });
    }

    @Override
    public void postUserLogList(int requestAction, String from, String to, Integer pageSize, Integer page) {
        getCommonObservable(requestAction, logObservable.userLogList(from, to, pageSize, page))
                .subscribe(new Consumer<LogItemWo>() {
                    @Override
                    public void accept(@NonNull LogItemWo logItemWo) throws Exception {
                        userLogListView.responseSuccess(requestAction, logItemWo);
                    }
                });
    }

    @Override
    public void postLogout(int requestAction) {
        getCommonObservable(requestAction, loginedObservable.logout(""))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        homeListenenr.responseSuccess(requestAction, statusWo);
                    }
                });
    }

    @Override
    public void postPlatformUpdate(int requestAction) {
        Integer platformID = HttpConfigure.PLATFORM_TYPE_ANDROID;
        Integer version = AppUtil.getVersionCode(MyApplication.getContext());
        getCommonObservable(requestAction, userUnLoginObservable.platformUpdate(platformID, version))
                .subscribe(updateInfoWo -> homeListenenr.responseSuccessGetUpdateInfoWo(requestAction, updateInfoWo));
    }
}
